<?php

namespace App\Http\Controllers\Admin;

use App\Model\Advice;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Config;

class AdviceController extends Controller
{
    /**
     * @return mixed
     * 淘宝资讯列表
     */
    public function index(){
        $advice = new Advice();
        $adviceInfo = $advice->orderBy('created_at','desc')->get();
        return view('admin.advice.index',[
            'adviceInfo' => $adviceInfo
        ]);
    }

    /**
     * 资讯新增
     */
    public function add(){
        return view('admin.advice.add');
    }
    /**
     * 新增结果
     */
    public function addRes(Request $request){
        $data = $request->all();
        $advice = new Advice();
        $advice->title = $data['title'];
        $advice->author = $data['author'];
        $advice->look_num = $data['look_num'];
        $advice->like_num = $data['like_num'];
        $advice->content = $data['content'];
        $advice->type = $data['type'];
        if($advice->type == 0){
            $advice->imgurl = $this->upImg($request,'imgurl');
        }elseif ($advice->type == 1){
            $advice->imgurl = '/uploads/goods/advice/yulu.jpg';
        }else{
            $advice->imgurl = '/uploads/goods/advice/kejian.jpg';
        }
        $res = $advice->save();
        if($res){
            return redirect('admin/advice-list')->with('success','新增成功');
        }else{
            return back()->with('error','新增失败');
        }
    }

    /**
     * @param Request $request
     * 商品修改
     */
    public function update($id){
        $advice = new Advice();
        $adviceInfo = $advice->find($id);
        return view('admin/.advice.update',[
            'adviceInfo' => $adviceInfo
        ]);
    }

    /**
     * @param Request $request
     * @return mixed
     * 商品修改结果
     */
    public function updateRes(Request $request){
        $advice = new Advice();
        $data = $request->all();
        $adviceInfo = $advice->find($data['id']);
        $adviceInfo->title = $data['title'];
        $adviceInfo->author = $data['author'];
        $adviceInfo->look_num = $data['look_num'];
        $adviceInfo->like_num = $data['like_num'];
        $adviceInfo->content = $data['content'];
        if(!empty($data['imgurl'])){
            @unlink('.'.$adviceInfo->imgurl);
            $adviceInfo->imgurl = $this->upImg($request,'imgurl');
        }
        $res = $adviceInfo->save();
        if($res){
            return redirect('/admin/advice-list')->with("success","修改成功");
        }else{
            return back()->with("error",'修改失败，请稍后再试');
        }
    }

    /**
     * 图片上传
     */
    private function upImg($request,$name){
        if($request->hasFile($name)){
            $path = Config::get('app.goodsImgPath');
            $fileName = time().rand(10000,999999).'.'.$request->file($name)->getClientOriginalExtension();
            $request->file($name)->move($path, $fileName);
            return substr($path.$fileName,1);
        }
    }
    /**
     * @param Request $request
     * 异步删除
     */
    public function delete(Request $request){
        $advice = new Advice();
        $id = $request->input("advice_id");
        $res = $advice->where('id',$id)->delete();
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }
}
